<?php

namespace App\Console\Commands;

use App\Http\Controllers\Controller;
use App\Models\Store;
use Illuminate\Console\Command;
date_default_timezone_set('PRC');

class InitAvailableBooking extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'init_available_booking';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建可预约日期表格';

    protected $redis;

    protected $storeModel;

    protected $futureDay = 400;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->storeModel = new Store();
        $controller = new Controller();
        $this->redis = $controller->initRedis(3);
        parent::__construct();
    }

    public $timing = [
        '10:00',
        '10:30',
        '11:00',
        '11:30',
        '12:00',
        '12:30',
        "13:00",
        "13:30",
        "14:00",
        "14:30",
        "15:00",
        "15:30",
        "16:00",
        "16:30",
        "17:00",
        "17:30",
        "18:00",
        "18:30",
        "19:00",
        "19:30",
        "20:00",
        "20:30",
        "21:00"
    ];

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //判断key的数量是否小于futureDay
        if(count($this->redis->keys('*')) < $this->futureDay){
            $this->init();
        }else{
            //更新当天日期下的可预约时间的状态
            $this->updateTodayAvailableBookingTime();
        }

    }

    //更新当天日期下的可预约时间的状态
    public function updateTodayAvailableBookingTime(){
        $today = date('Y/m/d');
        //获取今天所有日期下的所有门店的状态
        $res = $this->redis->hGetAll($today);

        //循环所有的时间点
        foreach($res as $k=>$v){
            $arr=[];
            //循环所有的门店
            foreach(json_decode($v) as $m=>$n){
                if($this->compareTimeWithNow(date('Y/m/d')." ".$k)){

                    $arr[$m] = $n;

                }else{

                    $arr[$m] = 0;
                }
            }

            $this->redis->hSet($today,$k,json_encode($arr));

        }
    }

    public function init(){

        $storeArr = $this->getStoreArr();

        //循环未来的天数
        for($i = 0 ; $i< $this->futureDay; $i++){

            //获取未来天数的日期
            $date = date("Y/m/d", strtotime("+" . $i . " day"));

            //循环时间点
            foreach ($this->timing as $m=>$n) {

                $arr = [];

                //循环门店
                foreach ($storeArr as $j=>$k){
                    if($i == 0){
                        $arr[$k] = $this->compareTimeWithNow($date." ".$n);
                    }else{
                        $arr[$k] = 1;
                    }
                }

                $this->redis->hSet($date, $n, json_encode($arr));
                $this->redis->expire($date,($i+1)*24*60*60);

            }

            echo "key为".$date."的数据生成完毕"."\n\r";


        }

    }

    /**
     * 对比参数(日期+时间）和现在哪个的时间戳比较大
     */
    public function compareTimeWithNow($dateAndTime){
        $dateTimestamp = strtotime($dateAndTime);
        if(strtotime("now") < $dateTimestamp){
            return 1;
        }else{
            return 0;
        }
    }


    /**
     * 获取所有门店名称集合成数组
     * @return array
     */
    public function getStoreArr(){
        $res = $this->storeModel->getInfoList();
        $arr = [];
        foreach ($res as $k=>$v){
            $arr[$k] = $v['name'];
        }
        return $arr;
    }



}
